<template>
    <div class="box">
        <div class="topBtn">
            <van-button type="info" round size="mini" @click="handleBack()">关闭 X</van-button>
        </div>
        <img src="../../assets/images/loading.gif">
        <h5>欢迎使用智慧后勤服务保障系统</h5>
        <p>页面加载中，请耐心等候......</p>
        <div class="scheduleBox">
            <div id="scheduleMsg"></div>
        </div>
    </div>
</template>

<script>
    import Setting from "../../setting";
    import { getCode, getTicket, getLoginUserInfo } from "@/api/isc.js";

    export default {
        created () {
            let appCode = this.$route.params.appCode || '';
            if (appCode) {
                this.getTicketData(appCode);
            }
        },
        methods: {
            // 获取ticket
            getTicketData (appCode) {
                getTicket(appCode).then(res => {
                    if (res && res.data && res.data.data) {
                        let ticket = '';
                        if (Setting.codeMode === "uapRelease") { // 生产环境
                            ticket = res.data.data;
                        } else { // 测试环境
                            ticket = res.data.data.ticket;
                        }
                        this.getUserInfo(ticket);
                    } else {
                        getCode();
                    }
                }).catch(() => {
                    getCode();
                })
            },
            // 获取用户信息
            getUserInfo (ticket) {
                let scheduleMsg = document.getElementById('scheduleMsg');
                if (
                    Setting.codeMode == "norRelease" ||
                    sessionStorage.getItem("UAP_ISC_PORT")
                ) {
                    scheduleMsg.style.width = "80%";
                    getLoginUserInfo(ticket)
                        .then((res) => {
                            scheduleMsg.style.width = "100%";
                            if (res && res.code == 1000 && res.data) {
                                let _data = { ...res.data, ...res.data.custom, userId: res.data.user };
                                sessionStorage.setItem("userInfo", JSON.stringify(_data));
                                this.$store.commit("setUserInfo", _data);
                                sessionStorage.setItem("userid", res.data.user);
                                sessionStorage.setItem("token", res.data.token);
                                this.$router.push('/');
                            } else if (res && res.code === 2000 && res.msg.indexOf('未授权') !== -1) {
                                this.$Dialog.alert({
                                    message: '该用户无权限，请联系系统技术服务人员处理！',
                                    confirmButtonText: '返回工作台'
                                }).then(() => {
                                    wx.closeWindow();
                                });
                            } else {
                                if (res.msg) {
                                    this.$Dialog.alert({
                                        message: res.msg,
                                        confirmButtonText: '返回工作台'
                                    }).then(() => {
                                        wx.closeWindow();
                                    });
                                } else {
                                    this.$Dialog.alert({
                                        message: '登录异常',
                                        confirmButtonText: '返回工作台'
                                    }).then(() => {
                                        wx.closeWindow();
                                    });
                                }
                            }
                        })
                        .catch(() => {
                            this.$Dialog.alert({
                                message: '登录异常',
                                confirmButtonText: '返回工作台'
                            }).then(() => {
                                wx.closeWindow();
                            });
                        });
                } else {
                    let timer = setInterval(() => {
                        if (sessionStorage.getItem("UAP_ISC_PORT")) {
                            scheduleMsg.style.width = "80%";
                            // 获取用户信息
                            getLoginUserInfo(ticket)
                                .then((res) => {
                                    scheduleMsg.style.width = "100%";
                                    if (res && res.code === 1000 && res.data) {
                                        let _data = { ...res.data, ...res.data.custom, userId: res.data.user };
                                        sessionStorage.setItem("userInfo", JSON.stringify(_data));
                                        this.$store.commit("setUserInfo", _data);
                                        sessionStorage.setItem("userid", res.data.user);
                                        sessionStorage.setItem("token", res.data.token);
                                        this.$router.push('/');
                                    } else if (res && res.code === 2000 && res.msg.indexOf('未授权') !== -1) {
                                        this.$Dialog.alert({
                                            message: '该用户无权限，请联系系统技术服务人员处理！',
                                            confirmButtonText: '返回工作台'
                                        }).then(() => {
                                            wx.closeWindow();
                                        });
                                    } else {
                                        if (res.msg) {
                                            this.$Dialog.alert({
                                                message: res.msg,
                                                confirmButtonText: '返回工作台'
                                            }).then(() => {
                                                wx.closeWindow();
                                            });
                                        } else {
                                            this.$Dialog.alert({
                                                message: '登录异常',
                                                confirmButtonText: '返回工作台'
                                            }).then(() => {
                                                wx.closeWindow();
                                            });
                                        }
                                    }
                                })
                                .catch(() => {
                                    this.$Dialog.alert({
                                        message: '登录异常',
                                        confirmButtonText: '返回工作台'
                                    }).then(() => {
                                        wx.closeWindow();
                                    });
                                });
                            clearInterval(timer);
                        }
                    }, 1000);
                }
            },
            // 返回工作台
            handleBack () {
                wx.closeWindow();
            }
        }
    }
</script>


<style scoped lang="less">
    .box {
        height: 100%;
        overflow: hidden;
        text-align: center;
        .topBtn {
            text-align: right;
            padding: 15px;
            box-sizing: border-box;
            .van-button--round {
                padding: 0 8px;
            }
        }
        img {
            margin-top: 42%;
            width: 60%;
        }
        h5 {
            font-size: 15px;
            line-height: 30px;
            color: #333;
        }
        p {
            font-size: 13px;
            color: #666;
        }
        .scheduleBox {
            width: 75%;
            height: 12px;
            background: rgb(231, 231, 231);
            border-radius: 10px;
            margin: 20px auto 5px;
            overflow: hidden;
            border: 2.5px solid rgb(231, 231, 231);
            div {
                background: #79b5f2;
                width: 0;
                height: 100%;
                animation: 1.5s;
            }
        }
    }
</style>